package com.person.jc.web.base.car;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.base.car.BdCarInfoService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class BdCarInfoController extends Controller {

	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BdCarInfoService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String carId = getPara("carId");
		String view = getPara("view");
		if(Utils.isNotEmpty(carId)){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("carId", carId);
			List<Record> list = BdCarInfoService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	
	public void save(){
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String carId = String.valueOf(map.get("carId"));
		try {
			if(Utils.isNotEmpty(carId)){
				BdCarInfoService.me.update(map);
			}else{
				BdCarInfoService.me.save(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}
	
	public void delete(){
		String res = "success";
		String carIds = getPara("carIds");
		try {
			for (String carId : carIds.split(",")) {
				BdCarInfoService.me.delete(carId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}
	
	public void checkName(){
		String carCode = getPara("carCode");
		String carId = getPara("carId");
		long count = BdCarInfoService.me.checkName(carCode, carId);
		renderText(count == 0 ? "success" : "error");
	}
	
	
}
